home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / c_rehash < prev    next >
Encoding:
Text File  |  2012-05-16  |  3.8 KB  |  168 lines

  1. #!/usr/bin/perl
  2.  
  3.  
  4. # Perl c_rehash script, scan all files in a directory
  5. # and add symbolic links to their hash values.
  6.  
  7. my $openssl;
  8.  
  9. my $dir = "/usr/lib/ssl";
  10. my $prefix = "/usr";
  11.  
  12. if(defined $ENV{OPENSSL}) {
  13.     $openssl = $ENV{OPENSSL};
  14. } else {
  15.     $openssl = "openssl";
  16.     $ENV{OPENSSL} = $openssl;
  17. }
  18.  
  19. $ENV{PATH} .= ":$dir/bin";
  20.  
  21. if(! -x $openssl) {
  22.     my $found = 0;
  23.     foreach (split /:/, $ENV{PATH}) {
  24.         if(-x "$_/$openssl") {
  25.             $found = 1;
  26.             last;
  27.         }    
  28.     }
  29.     if($found == 0) {
  30.         print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
  31.         exit 0;
  32.     }
  33. }
  34.  
  35. if(@ARGV) {
  36.     @dirlist = @ARGV;
  37. } elsif($ENV{SSL_CERT_DIR}) {
  38.     @dirlist = split /:/, $ENV{SSL_CERT_DIR};
  39. } else {
  40.     $dirlist[0] = "$dir/certs";
  41. }
  42.  
  43.  
  44. foreach (@dirlist) {
  45.     if(-d $_ and -w $_) {
  46.         hash_dir($_);
  47.     }
  48. }
  49.  
  50. sub hash_dir {
  51.     my %hashlist;
  52.     print "Doing $_[0]\n";
  53.     chdir $_[0];
  54.     opendir(DIR, ".");
  55.     my @flist = readdir(DIR);
  56.     # Delete any existing symbolic links
  57.     foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
  58.         if(-l $_) {
  59.             unlink $_;
  60.         }
  61.     }
  62.     closedir DIR;
  63.     FILE: foreach $fname (grep {/\.pem$|\.crt$/} @flist) {
  64.         # Check to see if certificates and/or CRLs present.
  65.         my ($cert, $crl) = check_file($fname);
  66.         if(!$cert && !$crl) {
  67.             ($cert, $crl) = check_file("$openssl x509 -in \"$fname\" -inform der  -outform pem | ");
  68.             if(!$cert && !$crl) {
  69.                 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
  70.                 next;
  71.             }
  72.         }
  73.         link_hash_cert($fname) if($cert);
  74.         link_hash_crl($fname) if($crl);
  75.     }
  76. }
  77.  
  78. sub check_file {
  79.     my ($is_cert, $is_crl) = (0,0);
  80.     my $fname = $_[0];
  81.     open IN, $fname;
  82.     while(<IN>) {
  83.         if(/^-----BEGIN (.*)-----/) {
  84.             my $hdr = $1;
  85.             if($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
  86.                 $is_cert = 1;
  87.                 last if($is_crl);
  88.             } elsif($hdr eq "X509 CRL") {
  89.                 $is_crl = 1;
  90.                 last if($is_cert);
  91.             }
  92.         }
  93.     }
  94.     close IN;
  95.     return ($is_cert, $is_crl);
  96. }
  97.  
  98.  
  99. # Link a certificate to its subject name hash value, each hash is of
  100. # the form <hash>.<n> where n is an integer. If the hash value already exists
  101. # then we need to up the value of n, unless its a duplicate in which
  102. # case we skip the link. We check for duplicates by comparing the
  103. # certificate fingerprints
  104.  
  105. sub link_hash_cert {
  106.         my $fname = $_[0];
  107.         $fname =~ s/'/'\\''/g;
  108.         my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in '$fname'`;
  109.         if(!$hash || !fprint) {
  110.             ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in '$fname' -inform der`;
  111.         }
  112.         chomp $hash;
  113.         chomp $fprint;
  114.         $fprint =~ s/^.*=//;
  115.         $fprint =~ tr/://d;
  116.         my $suffix = 0;
  117.         # Search for an unused hash filename
  118.         while(exists $hashlist{"$hash.$suffix"}) {
  119.             # Hash matches: if fingerprint matches its a duplicate cert
  120.             if($hashlist{"$hash.$suffix"} eq $fprint) {
  121.                 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
  122.                 return;
  123.             }
  124.             $suffix++;
  125.         }
  126.         $hash .= ".$suffix";
  127.         print "$fname => $hash\n";
  128.         $symlink_exists=eval {symlink("",""); 1};
  129.         if ($symlink_exists) {
  130.             symlink $fname, $hash;
  131.         } else {
  132.             system ("cp", $fname, $hash);
  133.         }
  134.         $hashlist{$hash} = $fprint;
  135. }
  136.  
  137. # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
  138.  
  139. sub link_hash_crl {
  140.         my $fname = $_[0];
  141.         $fname =~ s/'/'\\''/g;
  142.         my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout -in '$fname'`;
  143.         chomp $hash;
  144.         chomp $fprint;
  145.         $fprint =~ s/^.*=//;
  146.         $fprint =~ tr/://d;
  147.         my $suffix = 0;
  148.         # Search for an unused hash filename
  149.         while(exists $hashlist{"$hash.r$suffix"}) {
  150.             # Hash matches: if fingerprint matches its a duplicate cert
  151.             if($hashlist{"$hash.r$suffix"} eq $fprint) {
  152.                 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
  153.                 return;
  154.             }
  155.             $suffix++;
  156.         }
  157.         $hash .= ".r$suffix";
  158.         print "$fname => $hash\n";
  159.         $symlink_exists=eval {symlink("",""); 1};
  160.         if ($symlink_exists) {
  161.             symlink $fname, $hash;
  162.         } else {
  163.             system ("cp", $fname, $hash);
  164.         }
  165.         $hashlist{$hash} = $fprint;
  166. }
  167.  
  168.